package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.index.pojo.dto.IndexBoardChartGetConfigDTO;
import com.xbongbong.pro.index.pojo.dto.IndexBoardChartSaveConfigDTO;
import com.xbongbong.pro.index.pojo.dto.IndexBoardGetConfigDTO;
import com.xbongbong.pro.index.pojo.dto.IndexBoardHideDTO;
import com.xbongbong.pro.index.pojo.dto.IndexBoardInitDTO;
import com.xbongbong.pro.index.pojo.dto.IndexBoardSaveConfigWebDTO;
import com.xbongbong.pro.index.pojo.dto.IndexSaveFilterDTO;
import com.xbongbong.pro.index.pojo.vo.IndexBoardChartGetConfigVO;
import com.xbongbong.pro.index.pojo.vo.IndexBoardChartSaveConfigVO;
import com.xbongbong.pro.index.pojo.vo.IndexBoardGetConfigVO;
import com.xbongbong.pro.index.pojo.vo.IndexBoardHideVO;
import com.xbongbong.pro.index.pojo.vo.IndexBoardInitVO;
import com.xbongbong.pro.index.pojo.vo.IndexBoardSaveConfigVO;
import com.xbongbong.pro.index.pojo.vo.IndexSaveFilterVO;
import com.xbongbong.saas.service.IndexService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 首页看板
 * @Author: hongxiao
 * @date: 2019-02-26 22:00
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/index")
public class IndexController {

    @Resource
    private IndexService indexService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 首页看板初始化
     * @param boardInitDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-26 22:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String boardInit(@RequestBody @Valid IndexBoardInitDTO boardInitDTO, BindingResult br) {
        XbbResponse<IndexBoardInitVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                IndexBoardInitVO boardInitWebVO = indexService.boardInitWeb(boardInitDTO);
                response = new XbbResponse<>(boardInitWebVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 首页获取看板配置
     * @param getConfigDTO
     * @param br
     * @return 
     * @throws 
     * 
     * @author hongxiao
     * @date: 2019-02-26 22:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/getConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConfig(@RequestBody @Valid IndexBoardGetConfigDTO getConfigDTO, BindingResult br) {
        XbbResponse<IndexBoardGetConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                IndexBoardGetConfigVO getConfigWebVO = indexService.getConfigWeb(getConfigDTO);
                response = new XbbResponse<>(getConfigWebVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 保存首页看板配置
     * @param saveConfigWebDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-27 10:03
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/saveConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConfig(@RequestBody @Valid IndexBoardSaveConfigWebDTO saveConfigWebDTO, BindingResult br) {
        XbbResponse<IndexBoardSaveConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(saveConfigWebDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(saveConfigWebDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                IndexBoardSaveConfigVO saveConfigVO = indexService.saveConfigWeb(saveConfigWebDTO);
                response = new XbbResponse<>(saveConfigVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取首页看板下指标配置
     * @param chartGetConfigDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-27 10:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/chart/getConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartGetConfig(@RequestBody @Valid IndexBoardChartGetConfigDTO chartGetConfigDTO, BindingResult br) {
        XbbResponse<IndexBoardChartGetConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                IndexBoardChartGetConfigVO chartGetConfigVO = indexService.chartGetConfig(chartGetConfigDTO);
                response = new XbbResponse<>(chartGetConfigVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 保存首页看板下指标配置
     * @param chartSaveConfigDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-27 10:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/chart/saveConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartSetConfig(@RequestBody @Valid IndexBoardChartSaveConfigDTO chartSaveConfigDTO, BindingResult br) {
        XbbResponse<IndexBoardChartSaveConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(chartSaveConfigDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(chartSaveConfigDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                IndexBoardChartSaveConfigVO chartSaveConfigVO = indexService.chartSaveConfig(chartSaveConfigDTO);
                response = new XbbResponse<>(chartSaveConfigVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 隐藏首页看板
     * @param boardHideDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-27 10:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/board/hide", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String boardHide(@RequestBody @Valid IndexBoardHideDTO boardHideDTO, BindingResult br) {
        XbbResponse<IndexBoardHideVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                IndexBoardHideVO boardHideVO = indexService.boardHide(boardHideDTO);
                response = new XbbResponse<>(boardHideVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 保存首页看板筛选项
     * @param saveFilterDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-27 10:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/saveFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveFilter(@RequestBody @Valid IndexSaveFilterDTO saveFilterDTO, BindingResult br) {
        XbbResponse<IndexSaveFilterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(saveFilterDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(saveFilterDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                IndexSaveFilterVO saveFilterVO = indexService.saveFilter(saveFilterDTO);
                response = new XbbResponse<>(saveFilterVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
